//
//  WQCPlanDetailedViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/22.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCPlanDetailedViewController.h"
#import "configuration.h"
#import "WQCDetalTableViewCell.h"
#import "myPlanModel.h"
@interface WQCPlanDetailedViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView  *detailTableView;
@property(nonatomic,strong)NSMutableArray * allplanArr;
@end

@implementation WQCPlanDetailedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allplanArr = [[NSMutableArray alloc]init];
    [self loadmyplan];
    self.navigationController.navigationBarHidden =NO;
    
    self.view.backgroundColor = WYCGlobalBg;
    [self setupNav];
    self.detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, kScreenW, kScreenH+20) style:UITableViewStyleGrouped];
    self.detailTableView.dataSource = self;
    self.detailTableView.delegate = self;
    self.detailTableView.sectionFooterHeight = 10;
    self.detailTableView.sectionHeaderHeight = 10;
    [self.view addSubview:self.detailTableView];
}
- (void)setupNav {
    
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"计划明细";
}
#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allplanArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString * strID = @"cell";
    
    WQCDetalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WQCDetalTableViewCell" owner:self options:nil]lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    myPlanModel * DetailModel = self.allplanArr[indexPath.row];
    cell.creatTimeLb.text = DetailModel.executeTime;
    cell.poundageLb.text=DetailModel.poundage;
    NSString  *type =DetailModel.type;
    if ([type isEqualToString:@"1"]) {
        cell.typeLb.image = [UIImage imageNamed:@"consumption"];
        cell.amountLb.text = [NSString stringWithFormat:@"-%.1f",[DetailModel.amount doubleValue]];
        
    }
    else
    {
        cell.typeLb.image = [UIImage imageNamed:@"repayment"];
        cell.amountLb.text = [NSString stringWithFormat:@"+%.1f",[DetailModel.amount doubleValue]];
        
        
    }
    NSString  *state = DetailModel.state;
    if ([state isEqualToString:@"1"]) {
        cell.stateLb.text = @"待执行";
        
    }
    else if ([state isEqualToString:@"2"])
    {
        cell.stateLb.text = @"执行中";
        
    }
    else if ([state isEqualToString:@"3"])
    {
        cell.stateLb.text = @"执行成功";
        
    }else if ([state isEqualToString:@"4"])
    {
        cell.stateLb.text = @"执行失败";
        cell.stateLb.textColor = [UIColor redColor];
        
    }else if ([state isEqualToString:@"5"])
    {
        cell.stateLb.text = @"执行失败";
        cell.stateLb.textColor = [UIColor redColor];
        
    }
    else if ([state isEqualToString:@"6"])
    {
        cell.stateLb.text = @"还款终止";
        cell.stateLb.textColor = [UIColor redColor];
        
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadmyplan
{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"planId"] = self.planid;
    params[@"cardId"] = self.cardid;
    NSLog(@"%@",params);
    [HttpTool POST:getCardPlanDetail params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"计划明细%@",responseObject);
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            NSArray * dataArray = responseObject[@"result"];
            for (NSDictionary * dic in dataArray) {
                
                myPlanModel * DetailModel = [myPlanModel mj_objectWithKeyValues:dic];
                
                [self.allplanArr addObject:DetailModel];
            }
            
        }
        [self.detailTableView reloadData];
        
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


@end
